layui.use(['layer', 'laydate'], function () {
    // $ = layui.jquery;
    var layer = layui.layer,
        laydate = layui.laydate;

    laydate.render({
        elem: '#recordDate',
        value: new Date()
    });
});

function getStuInfo() {
    // 获取表单参数
    var stuId = $("#stuId").val();
    var param = {
        stuId: stuId
    }
    if (!$.isEmptyObject(param.stuId)) {
        $.ajax({
            type: "POST",
            url: "/student/getStuInfoById",
            //contentType: "application/json",
            sync: true,
            data: param,
            dataType: "json",
            success: function (result) {
                console.log(result)
                if (result.code == 200) {
                    var stu = result.data;
                    $("#stuName").val(stu.stuName);
                } else {

                }
            }
        });
    }
}

function tempRecord() {
    // 获取表单参数
    var recordDate = $("#recordDate").val();
    var stuId = $("#stuId").val();
    var stuName = $("#stuName").val();
    var stuTemp = $("#stuTemp").val();
    var symptom1 = $("input[name='symptom1']:checked").val();
    var symptom2 = $("input[name='symptom2']:checked").val();
    var param = {
        recordDate: recordDate,
        stuId: stuId,
        stuName: stuName,
        stuTemp: stuTemp,
        symptom1: symptom1,
        symptom2: symptom2
    }
    // 检验表单参数
    var verifyResult = verify(param);
    if (verifyResult) {
        // 校验成功，执行添加请求
        $.ajax({
            type: "POST",
            url: "/temp/tempRecord",
            contentType: "application/json",
            sync: true,
            data: JSON.stringify(param),
            dataType: "json",
            success: function (result) {
                if (result.code == 200) {
                    layer.msg('体温登记成功！', {icon: 1, offset: '100px', time: 2000});
                    setTimeout(() => {
                        window.location.href = "index";
                    }, 2000);
                } else {
                    layer.msg(result.msg, {icon: 2, offset: '100px', time: 2000});
                }
            }
        });
    }
}

function verify(param) {
    var recordDate = param.recordDate;
    var stuId = param.stuId;
    var stuName = param.stuName;
    var stuTemp = param.stuTemp;
    var symptom1 = param.symptom1;
    var symptom2 = param.symptom2;

    if ($.isEmptyObject(recordDate)) {
        layer.msg('请填写日期！', {icon: 2, offset: '100px', time: 2000});
        return false;
    } else if ($.isEmptyObject(stuId)) {
        layer.msg('请填写学号！', {icon: 2, offset: '100px', time: 2000});
        return false;
    } else if ($.isEmptyObject(stuName)) {
        layer.msg('请填写姓名！', {icon: 2, offset: '100px', time: 2000});
        return false;
    } else if ($.isEmptyObject(stuTemp)) {
        layer.msg('请填写体温！', {icon: 2, offset: '100px', time: 2000});
        return false;
    } else if ($.isEmptyObject(symptom1)) {
        layer.msg('请选择是否出现咳嗽等症状！', {icon: 2, offset: '100px', time: 2000});
        return false;
    } else if ($.isEmptyObject(symptom2)) {
        layer.msg('请选择是否出现气促等症状！', {icon: 2, offset: '100px', time: 2000});
        return false;
    }
    return true;
}